/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-09 17:24:07
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.bp.bean.BpExamAssessEditBean;
import com.joysuch.wwyt.bp.bean.BpExamAssessListBean;
import com.joysuch.wwyt.bp.bean.BpExamAssessListBean.Search;
import com.joysuch.wwyt.bp.service.BpExamAssessService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 考核评审管理——考核评审
 */
@RestController
@Api(tags = "核评审管理")
@RequestMapping("/wwyt/bp-exam-assess")
public class BpExamAssessController {

    private static final Logger logger = LoggerFactory.getLogger(BpExamAssessController.class);

    @Autowired
    private BpExamAssessService bpExamAssessService;

    /**
     * 添加考核
     *
     * @param entity
     *            待添加的考核信息
     * @throws Exception
     */
    @RequestMapping("add")
    @RequiresPermissions("wwyt:bp-exam-assess:add")
    public Object add(@Valid BpExamAssessEditBean entity) throws Exception {
        bpExamAssessService.add(entity);
        return ResultBean.success("");
    }

    /**
     * 修改考核
     *
     * @param entity
     *            待修改的考核信息
     */
    @RequestMapping("update")
    @RequiresPermissions("wwyt:bp-exam-assess:update")
    public Object update(@Valid BpExamAssessEditBean entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpExamAssessService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    /**
     * 获取考核信息列表（分页）
     *
     * @param page
     *            分页信息
     * @param condition
     *            筛选条件[examPersonId]
     */
    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            Page<BpExamAssessListBean> data = bpExamAssessService.findByPage(page, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    /**
     * 删除考核
     *
     * @param id
     *            待删除的考核 ID
     */
    @RequestMapping("delete")
    @RequiresPermissions("wwyt:bp-exam-assess:delete")
    public Object delete(Long id) {
        try {
            bpExamAssessService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    /**
     * 获取指定考核
     *
     * @param id
     *            指定考核 ID
     */
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(bpExamAssessService.getById(id));
    }
}
